<?php
/**
 * Created by PhpStorm.
 * User: M2
 * Date: 13-11-13
 * Time: 下午8:11
 */
class YlIfTaglib {

    public function parse($content, $arr) {
        if (stripos($content, 'yl:if') !== false) {
            $matches = preg_parse($content, 'yl:if');
            foreach ($matches as $match) {
                $testType = getParseOneAttr($match, 'testType');
                $testOperate = getParseOneAttr($match, 'testOperate');
                $testValue = getParseOneAttr($match, 'testValue');
                $text = getParseContentText($match, 'yl:if');
                //OOO fix by jepin 20140916
                $successTemplate = preg_parse($match, 'yl:successTemplate');  
                $failureUemplate = preg_parse($match, 'yl:failureTemplate');  
                //$successTemplate = preg_parse($content, 'yl:successTemplate');
                //$failureUemplate = preg_parse($content, 'yl:failureTemplate');
                if (strtolower($testOperate) === 'in') {
                    if (strpos($testValue, $arr[$testType]) !== false) {
                        $text = taglib(C('yl:successTemplate'))->parse($text, $arr, 1);
                        $text = str_ireplace($failureUemplate[0], '', $text);
                    } else {
                        $text = taglib(C('yl:failureTemplate'))->parse($text, $arr, 1);
                        $text = str_ireplace($successTemplate[0], '', $text);
                    }
                } elseif (strtolower($testOperate) === 'equals') {
                    if ($arr[$testType] === $testValue) {
                        $text = taglib(C('yl:successTemplate'))->parse($text, $arr, 1);
                        $text = str_ireplace($failureUemplate[0], '', $text);
                    } else {
                        $text = taglib(C('yl:failureTemplate'))->parse($text, $arr, 1);
                        $text = str_ireplace($successTemplate[0], '', $text);
                    }
                } elseif (strtolower($testOperate) === 'notempty') {
                    if (!empty($arr[$testType])) {
                        $text = taglib(C('yl:successTemplate'))->parse($text, $arr, 1);
                        $text = str_ireplace($failureUemplate[0], '', $text);
                    } else {
                        $text = taglib(C('yl:failureTemplate'))->parse($text, $arr, 1);
                        $text = str_ireplace($successTemplate[0], '', $text);
                    }
                } elseif (strtolower($testOperate) === 'eq') {
                	$v = explode(',', $testValue);
                    if ($v[0]==$v[1]) {
                        $text = taglib(C('yl:successTemplate'))->parse($text, $arr, 1);
                        $text = str_ireplace($failureUemplate[0], '', $text);
                    } else {
                        $text = taglib(C('yl:failureTemplate'))->parse($text, $arr, 1);
                        $text = str_ireplace($successTemplate[0], '', $text);
                    }
                }
                $content = str_replace($match, trim($text), $content);
            }
        }
        return $content;
    }
}